using Senparc.Weixin.Entities;
using Senparc.Weixin.Exceptions;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace Senparc.Weixin.HttpUtility
{
	public static class Get
	{
		public static T GetJson<T>(string url, Encoding encoding = null)
		{
			string text = RequestUtility.HttpGet(url, encoding);
			JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
			if (text.Contains("errcode"))
			{
				WxJsonResult wxJsonResult = javaScriptSerializer.Deserialize<WxJsonResult>(text);
				if (wxJsonResult.errcode != ReturnCode.请求成功)
				{
					throw new ErrorJsonResultException(string.Format("微信请求发生错误！错误代码：{0}，说明：{1}", (int)wxJsonResult.errcode, wxJsonResult.errmsg), null, wxJsonResult);
				}
			}
			return javaScriptSerializer.Deserialize<T>(text);
		}
		public static void Download(string url, Stream stream)
		{
			WebClient webClient = new WebClient();
			byte[] array = webClient.DownloadData(url);
			byte[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				byte value = array2[i];
				stream.WriteByte(value);
			}
		}
	}
}
